package com.lz.football.module.friend
{
	import com.common.utils.DisplayObjectUtil;
	import com.common.utils.Reflection;
	import com.common.utils.timeUtils.TimerManager;
	import com.global.GlobalDef;
	import com.liangzhong.constants.ClubConst;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.FriendEvent;
	import com.liangzhong.events.NotifierEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.chat.vo.ChatVo;
	import com.liangzhong.model.friend.vo.FriendInfoVo;
	import com.lz.football.module.club.item.BaseItem;
	import com.lz.football.module.playerCenter.ListItem;
	import com.lz.football.module.trainingcamp.TrainingcampPlayerItem;
	
	import common.BaseView.BaseView;
	import common.BaseView.DisplayMouseEventManager;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import lz.controls.Alert;

	/**
	 * 查看信息面板 
	 * @author Administrator
	 * 
	 */	
	public class LookUpInfoView extends BaseView implements ILookUpView
	{
		/**
		 *名称  
		 */
		private var playerName:TextField;
		/**
		 *国家 
		 */
		private var country:TextField;
		/**
		 *等级  
		 */
		private var Lv:TextField;
		/**
		 *综合能力 
		 */
		private var ability:MovieClip;
		/**
		 *联盟 
		 */
		private var alliance:TextField;
		/**
		 *球场
		 * 宽247  高319
		 *  
		 */
		private var ball:MovieClip;
		/**
		 *g球员容器 
		 */
		private var playerContain:MovieClip;
		/**
		 *成就窗口 
		 */
		private var achievementContain:MovieClip;
		/**
		 *天梯排名  
		 */
		private var ranking:TextField;
		/**
		 *添加 
		 */
		private var addBtn:SimpleButton;
		/**
		 *私聊 
		 */
		private var chatBtn:SimpleButton;
		/**
		 *挑战按钮 
		 */
		private var challengeBtn:SimpleButton;
		/**
		 *删除好友 
		 */
		private var deleteBtn:SimpleButton;
		/**
		 *队徽 
		 */
		private var teamIcon:MovieClip;
		/**
		 *取消挑战 
		 */
		private var cancelBtn:SimpleButton;
		/**
		 *倒计时 
		 */
		private var timeText:TextField;
		/**
		 *vip 
		 */
		private var vip:TextField;
		private var friendVo:FriendInfoVo
		/**
		 *是否正在挑战 
		 */		
		private var isChallengeIng :Boolean = false;
		public function LookUpInfoView()
		{
			super();
		}
		override public function initUI(view:Object=null):void
		{
			symbolSkin = Reflection.createInstance("com.football.module.friend.infoView");
			this.addChild(symbolSkin);
			playerName = symbolSkin.infoMc.playerName;
			country = symbolSkin.infoMc.country;
			Lv = symbolSkin.infoMc.Lv;
			ability = symbolSkin.infoMc.ability;
			alliance = symbolSkin.infoMc.alliance;
			ball = symbolSkin.ball;
			playerContain = symbolSkin.infoMc.playerContain;
			achievementContain = symbolSkin.infoMc.achievementContain;
			ranking = symbolSkin.infoMc.ranking;
			addBtn = symbolSkin.addBtn;
			chatBtn = symbolSkin.chatBtn; 
			challengeBtn = symbolSkin.challengeBtn;
			//challengeBtn.mouseEnabled = false;
			//challengeBtn.visible = false;
			_viewWidth = this.width;
			_viewHeight = this.height;
			deleteBtn = symbolSkin.deleteBtn;
			deleteBtn.visible = false;
			teamIcon = symbolSkin.infoMc.teamIcon;
			_windowBg = symbolSkin.windowBg;
			cancelBtn = symbolSkin.cancelBtn
			timeText = symbolSkin.timeText;
			vip = symbolSkin.infoMc.vip;
			isDrag = true;
			setCloseBtn(symbolSkin.closeBtn);
			onResizeHandler();
			addEvtListener();
//			challengeBtn.visible = false;
			cancelBtn.visible = false;
		}
		override public function addEvtListener():void
		{
			addBtn.addEventListener(MouseEvent.CLICK,onAddHandler);
//			cancelBtn.addEventListener(MouseEvent.CLICK,oncancelHandler);
			challengeBtn.addEventListener(MouseEvent.CLICK,onChallangeHandler);
			chatBtn.addEventListener(MouseEvent.CLICK,onChatHandler);
			deleteBtn.addEventListener(MouseEvent.CLICK,onDeleteHandler);
			
			
//			
		}
		
		protected function onDeleteHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			Alert.show("你确定要将" +friendVo.uname+ "移出你的好友名单吗？","",Alert.NO,GlobalDef.AlertContainer,deleteAlert);
		}
		/**
		 *删除  
		 * @param $type
		 * 
		 */
		private function deleteAlert($type:int):void
		{
			if($type == Alert.YES)
			{
				deleteBtn.visible = false;
				AppEventDispatcher.getInstance().dispatchEvent(new FriendEvent(FriendEvent.DELETE_FRIEND,{id:friendVo.friendid}));
			}
		}
		protected function onChatHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			var chatvo:ChatVo = new ChatVo();
			chatvo.name = friendVo.uname;
			chatvo.id = friendVo.friendid;
			AppEventDispatcher.getInstance().dispatchEvent(new NotifierEvent(NotifierEvent.PRIVATE_CHAT,{vo:chatvo}));
		}
		
		/**
		 *挑战 
		 * @param event
		 * 
		 */
		protected function onChallangeHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			if(!AppData.getInstance().friendManager.getisFrined(friendVo.friendid))
			{
				Alert.showErrorTip("对方还不是你的好友！");
				return;
			}
			
			
			if(AppData.getInstance().clubDatamanager.clubInfo.friendCount >0)
			{
				changeFriend(Alert.YES);
			}else
			{
				Alert.show("今天的挑战次数已用完，你确定花费20点券继续挑战吗?","",Alert.NO,GlobalDef.AlertContainer,changeFriend);
			}
			
			
			
		}
		private function changeFriend(type:int):void
		{
			if(type == Alert.YES)
			{
				var f:FriendInfoVo = new FriendInfoVo();
				f.friendid = friendVo.friendid;
				f.isRequestInfo = true;
				AppEventDispatcher.getInstance().dispatchEvent(new FriendEvent(FriendEvent.CHALLENGE_FRIEND,{vo:f,type:2}));
			}
		}
		/**
		 *取消挑战
		 * @param event
		 * 
		 */
		protected function oncancelHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			reset();
			var f:FriendInfoVo =friendVo
			AppEventDispatcher.getInstance().dispatchEvent(new FriendEvent(FriendEvent.CANCEL_CHALLENGE_FRIEND,{vo:f,type:2}));
		}
		
		/**
		 * 添加 好友 
		 * @param event
		 * 
		 */
		protected function onAddHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			if(friendVo.friendid == AppData.getInstance().clubDatamanager.clubInfo.clubid)
			{
				Alert.showErrorTip("自己不可以添加好友");
				return;
			}
			if(AppData.getInstance().friendManager.getFriendById(friendVo.friendid) != null)
			{
				Alert.showErrorTip(friendVo.uname + "已经是你的好友");
				return;
			}
			AppEventDispatcher.getInstance().dispatchEvent(new FriendEvent(FriendEvent.INVITE_FRIENDS,{array:[friendVo.friendid]}));
		}
		/**
		 *关闭 是判断是否有挑战状态
		 * @param event
		 * 
		 */
		override public function onCloseHandler(event:MouseEvent):void
		{
			if(isChallengeIng)
			{
				Alert.show("正确的等待挑战，是否退出","",Alert.NO,GlobalDef.AlertContainer,alert);
				function alert($type:int):void
				{
					if(Alert.YES)
					{
						oncancelHandler(event);
						removePanel();
					}
				}
				return;
				
			}
			removePanel();
			
		}
		private function removePanel():void
		{
			reset();
			if(this.parent)
				this.parent.removeChild(this);
		}
		/**
		 *显示信息 
		 * @param $obj
		 * 
		 */
		public function showInfo($obj:Object):void
		{
			while(playerContain.numChildren)
			{
				(playerContain.removeChildAt(0) as ListItem).destroyView();
			}
			while(ball.numChildren > 1)
			{
				(ball.removeChildAt(1) as ListItem).destroyView();
			}
			
			reset();
			if($obj.vo.isStartTime)
			{
				
//				setStatus();
			}
			if(!$obj.vo.isUpdata)//不需要更新界面上的数据
			{
				
				return;
			}
			friendVo = $obj.vo;
//			deleteBtn.visible = false;
//			addBtn.visible = false;
//			if(AppData.getInstance().friendManager.getFriendById(friendVo.friendid) != null)
//			{
//				deleteBtn.visible = true;
//			}else
//			{
//				addBtn.visible = true;
//			}
			playerName.text = friendVo.uname + "";
			if(friendVo.VIP > 0)
				vip.text = "VIP"+friendVo.VIP + "";
			else 
				vip.text = "";
			country.text = "国家："+ClubConst.countryToString(friendVo.country) + "";
			Lv.text = "Lv."+friendVo.level + "";
//			ability.text = friendVo.ability + "";
			var temp:int = Math.ceil(friendVo.ability / 20);
			ability.gotoAndStop(temp);
			
			alliance.text = "联盟：";
			ranking.text = "" + friendVo.ttsort;
			
			DisplayMouseEventManager.instance.addTipsDisplay(ability,"俱乐部综合能力：" + friendVo.ability);
			
			showChanglleTime();
			
			
			
			DisplayObjectUtil.removeAllChildren(teamIcon);
			var lo:MovieClip = Reflection.createInstance(AppData.getInstance().clubDatamanager.getTeamIconVo(friendVo.logo).iconurl);
			lo.scaleX = lo.scaleY = 0.4;
			teamIcon.addChild(lo);
			var arr:Array = friendVo.highPlayerList;
			var len:int;
			var i:int;
			var item:ListItem;
			len = arr.length;
			for(i = 0;i < len;i++)
			{
				item = new ListItem();
				item.initUI();
				item.removeBar();
				item.data = arr[i];
				item.isShowTips = true;
				item.x = (item.width + 20)* i;
				playerContain.addChild(item);
			}
			
			arr = friendVo.playerList;
			len = arr.length;
			for(i = 0 ;i < len;i++)
			{
				item = new ListItem();
				item.initUI();
				item.removeBar();
				item.data = arr[i];
				item.x = arr[i].flasx -25 ;
				item.y = arr[i].flasy - 33;
				item.isShowTips = true;
				ball.addChild(item);
			}
			arr = friendVo.achieveList;
			len =3// arr.length;
			var baseItem:BaseItem;
			for(i = 0;i < len;i++)
			{
				baseItem = new BaseItem();
				baseItem.initUI("com.lz.commonUI.ItemFrame");
//				achievementContain.addChild(baseItem);
				baseItem.x = (baseItem.width + 2)*i;
				
			}
		}
		/**
		 *挑战次数 
		 * 
		 */
		public function showChanglleTime():void
		{
			var str:String = "今日可以挑战次数10";
			if(AppData.getInstance().clubDatamanager.clubInfo.friendCount <= 0)
			{
				str = "挑战可获得双倍欧元!\n今日挑战次数用完成，再次挑战需要消耗20点券";
			}else
			{
				str = "挑战可获得双倍欧元!\n今日可挑战次数" + AppData.getInstance().clubDatamanager.clubInfo.friendCount;
			}
			//			str = "挑战可获得双倍欧元!\n今日挑战次数用完成，再次挑战需要消耗20点券";
			DisplayMouseEventManager.instance.addTipsDisplay(challengeBtn,str);
		}
		
		/**
		 *设置状态 
		 * 
		 */
		private function setStatus():void
		{
			challengeBtn.visible = false;
//			cancelBtn.visible = true;
			timeText.text = "0";
			isChallengeIng = true;
//			TimerManager.instance.addFunction(timeTip);
		}
			
		private function timeTip():void
		{
			var t :int = int(timeText.text);
//			timeText.text = (++t) + "";
		}
		public function reset():void
		{
//			challengeBtn.visible = true;
//			cancelBtn.visible = false;
			timeText.text = "";
			TimerManager.instance.removeFunction(timeTip);
			isChallengeIng = false;
			DisplayMouseEventManager.instance.removeTipsDisplay(ability);
		}
	}
}